﻿using System.Web.Mvc;
using ipFoo.Helpers;

namespace ipFoo.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// xml.ipfoo.com or /XML
        /// </summary>
        /// <returns></returns>
        public ContentResult Xml()
        {
            // Get the data
            var ipData = IpAddress.Get(onlyIp: true);

            return new ContentResult {Content = string.Format("<ip_address><ip>{0}</ip></ip_address>", ipData.IpAddress)};
        }

        /// <summary>
        /// text.ipfoo.com or /TEXT
        /// </summary>
        /// <returns></returns>
        public ContentResult Text()
        {
            // Get the data
            var ipData = IpAddress.Get(onlyIp:true);

            return new ContentResult { Content = ipData.IpAddress };
        }

        /// <summary>
        /// ipfoo.com Home
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            // Check host name data and look for our text/xml subdomains. If matched, return the data associated with them.
            // NOTE: Currently looking for a better way to handle this. If you know of one, let me know!
            if (Request.Url.Host == "xml.ipfoo.com")
                return Xml();
            if (Request.Url.Host == "text.ipfoo.com")
                return Text();

            // Isn't actually used in the page anymore, but keeping it around because it's funny.
            if (Request.RawUrl.Contains("aintnojibbajabba"))
                return RedirectToAction("Text");

            // Get/set our data
            ViewBag.ActionModel = new Models.AdvancedFeatureModel();
            var ipData = IpAddress.Get();
            ipData.UserAgent = Request.UserAgent;

            return View(ipData);
        }

        /// <summary>
        /// This executes when an advanced feature is used via BROWSER POSTBACK.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Index(Models.AdvancedFeatureModel model)
        {
            // Validate request
            var response = Advanced.Validate(model);
            ViewBag.ActionModel = response.Data;

            // Bad validation? Send it back.
            if (!response.IsValid)
            {
                ViewBag.ValidExecution = false;
                ViewBag.LookupVal = response.Data.Lookup;
                return Index();
            }

            // Execute the results and bag it for display.
            ViewBag.ResponseBlock = Advanced.Run(model);
            return Index();
        }

        /// <summary>
        /// This executes when an advanced feature is used via JAVASCRIPT POSTBACK.
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [ValidateInput(false)]
        public JsonResult Execute(Models.AdvancedFeatureModel model)
        {
            // Validate our form data
            var response = Advanced.Validate(model);

            // All good? Execute the core commands.
            if (response.IsValid)
                response.ExecutionResults = Advanced.Run(response.Data);

            // Wrap it and return.
            var jResponse = Json(response);
            jResponse.JsonRequestBehavior = JsonRequestBehavior.AllowGet;

            return jResponse;
        }
    }
}
